<template>
  <el-button type="primary" @click="bannerAdd">
    添加轮播图 +
  </el-button>
  <!-- v-loading 加载 -->
  <el-table :data="tableData" style="width: 100%" v-loading="loading">
    <el-table-column label="图片编号" prop="bannerid" />
    <el-table-column label="图片地址">
      <template #default="scope">
        <img :src="scope.row.img" height="120" alt="">
      </template>
    </el-table-column>
    <el-table-column label="图片跳转地址" prop="link" />
    <el-table-column label="图片文字" prop="alt" />
    <!-- <el-table-column label="是否展示" prop="address">
      <template>
        abc
      </template>
    </el-table-column> -->
    <el-table-column label="是否展示" width="180">
      <template #default="scope">
        <el-switch v-model="scope.row.flag" />
      </template>
    </el-table-column>
    <el-table-column align="right">
      <template #header>
        <el-input v-model="search" size="small" placeholder="Type to search" />
      </template>
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
        <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!-- 编辑 新增 -->
  <el-dialog v-model="dialogFormVisible" :title="isEdit ? '新增' : '编辑'">
    <el-form :model="form">
      <el-form-item label="图片地址" label-width="100px">
        <el-input v-model="form.img" autocomplete="off" />
      </el-form-item>
      <el-form-item label="图片文字" label-width="100px">
        <el-input v-model="form.alt" autocomplete="off" />
      </el-form-item>
      <el-form-item label="图片链接" label-width="100px">
        <el-input v-model="form.link" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="editSave">
          保存
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { computed, ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getBannerList, setBanner, delBanner } from '@/api/bannerList.js'
import { useRouter } from 'vue-router'

const isEdit = ref(true)
const loading = ref(true)
const search = ref('')
const dialogFormVisible = ref(false)
const form = reactive({
  img: '',
  alt: '',
  link: '',
})
const router = useRouter()

// 新增
const bannerAdd = async () => {
  form.img = ''
  form.alt = ''
  form.link = ''
  dialogFormVisible.value = true
  isEdit.value = true
}

// 修改
const handleEdit = (index, row) => {
  console.log(index, row)
  // 显示编辑内容
  form.img = row.img
  form.alt = row.alt
  form.link = row.link
  dialogFormVisible.value = true
  isEdit.value = false
}

// 保存编辑内容
const editSave = async () => {
  // 什么时候关闭，一定是数据传输成功的时候
  dialogFormVisible.value = false

  if (!isEdit.value) {
    // 修改接口
  } else {
    // 添加接口
    let res = await setBanner('/banner/add', {
      img: form.img,
      alt: form.alt,
      link: form.link
    })

    if (res.code == '200') {
      // 重新请求当前数据接口
      let res = await getBannerList('/banner/list')
      tableData.value = res.data
      // 路由不靠谱
      // router.push({ path: '/home/bannerList' })
    }
  }
}

// 删除
const handleDelete = (index, row) => {
  ElMessageBox.alert('您是否要删除？', '提示', {
    // if you want to disable its autofocus
    // autofocus: false,
    confirmButtonText: '确定',
    callback: async (action) => {
      if (action === 'confirm') {
        // 删除接口
        let res = await delBanner('/banner/delete', { bannerid: row.bannerid })
        // 是否删除成功
        if (res.code == '200') {
          tableData.value.splice(index, 1)
          ElMessage({
            type: 'success',
            message: `删除成功`,
          })
        }
      }
    },
  })
}



const tableData = ref([])

onMounted(async () => {
  let res = await getBannerList('/banner/list')
  // console.log(res)
  if (res.code == '200') {
    tableData.value = res.data
    loading.value = false
  }
})
</script>
